//Component Object
const app = getApp();
const { apiInstance } = app

Component({
  properties: {
    data: {
      type: Object,
      value: {},
      observer: function (e) {
        if (e) {
          console.log('shenBao',e.shenBao?.buildingType)

          this.setData({
            form: e.shenBao,
            layoutType: this.data.layoutTypeList.map(item => { if (e.shenBao?.layoutType && e.shenBao?.layoutType.includes(item.name)) { return item.name } }),
            facilityRenovationCurrent: e.shenBao?.facilityRenovation || [],
            layoutTypeCurrent: e.shenBao?.layoutType || [],
            buildingTypeCurrent: e.shenBao?.BuildingType ||[],
            buildingUseageCurrent: e.shenBao?.buildingUseage || [],
          })
        }
      }
    },
    isEdit: {
      type: Boolean,
      value: false,
      observer: function (e) {
        if (e) {
          this.setData({
            isEdit: e,
          })
        }
      }
    },
  },
  data: {
    form: {
      layoutType: [],
      BuildingType: [],
      buildingUseage: [],
      occupationalHealthType: [],
      includeSpecialEquipment: '',
      includeSpecialEquipmentExtra: '',
      facilityRenovation: [],
      facilityRenovationDesc: '',
      safeEquipment: '',
      safeEquipmentExtra: '',
      hazardousChemicals: '',
      hazardousChemicalstableData: [{
        word1: '',
        word2: '',
        word3: '',
        word4: '',
        word5: '',
      }],
    },
    isEdit: false,
    layoutTypeCurrent: [],
    buildingUseageCurrent: [],
    buildingTypeCurrent: [],
    facilityRenovationCurrent: [],
    layoutTypeList: [{
      id: 1,
      name: '总平面布局',
    }, {
      id: 2,
      name: '建筑平面设计'
    }, {
      id: 3,
      name: '生产工艺/设备说明'
    }],

    buildingTypeList: [{
      id: 1,
      name: '甲',
    }, {
      id: 2,
      name: '乙'
    }, {
      id: 3,
      name: '丙'
    },
    {
      id: 4,
      name: '丁'
    },
    {
      id: 5,
      name: '戊'
    }],

    buildingUseageList: [{
      id: 1,
      name: '厂房'
    }, {
      id: 2,
      name: '仓库'
    }, {
      id: 3,
      name: '民用(办公、居住)'
    }],

    facilityRenovationList: [{
      id: 1,
      name: '委托施工单位办理'
    }, {
      id: 2,
      name: '公司自办'
    }, {
      id: 3,
      name: '使用的工艺及隔间材料防火性能说明'
    }]
  },
  methods: {
    handleLayoutTypeChange({ detail }) {

      this.data.layoutTypeCurrent ??= [];

      const index = this.data.layoutTypeCurrent?.indexOf(detail.value);
      (index == -1 || index == 'undefined') ? this.data.layoutTypeCurrent.push(detail.value) : this.data.layoutTypeCurrent.splice(index, 1);
      this.setData({
        layoutTypeCurrent: this.data.layoutTypeCurrent,
        'form.layoutType': this.data.layoutTypeCurrent
      });
    },
    handleBuildingTypeListChange({ detail }) {
      this.data.buildingTypeCurrent ??= [];
      const index = this.data.buildingTypeCurrent?.indexOf(detail.value);
      (index == -1 || index == 'undefined') ? this.data.buildingTypeCurrent.push(detail.value) : this.data.buildingTypeCurrent.splice(index, 1);
      this.setData({
        buildingTypeCurrent: this.data.buildingTypeCurrent,
        'form.BuildingType': this.data.buildingTypeCurrent
      });
    },

    handleBuildingUseageListChange({ detail }) {
      this.data.buildingUseageCurrent ??= [];
      const index = this.data.buildingUseageCurrent?.indexOf(detail.value);
      (index == -1 || index == 'undefined') ? this.data.buildingUseageCurrent.push(detail.value) : this.data.buildingUseageCurrent.splice(index, 1);
      this.setData({
        buildingUseageCurrent: this.data.buildingUseageCurrent,
        'form.buildingUseage': this.data.buildingUseageCurrent
      });
    },

    handleFacilityRenovationChange({ detail }) {
      this.data.facilityRenovationCurrent ??= [];
      const index = this.data.facilityRenovationCurrent?.indexOf(detail.value);
      (index == -1 || index == 'undefined') ? this.data.facilityRenovationCurrent.push(detail.value) : this.data.facilityRenovationCurrent.splice(index, 1);
      this.setData({
        facilityRenovationCurrent: this.data.facilityRenovationCurrent,
        'form.facilityRenovation': this.data.facilityRenovationCurrent
      });
    },

    imageUpload(e) {
      this.setData({
        ['form.uploadFileZiBan']: e.detail
      })
    },

    

    
    bindValue(e){

      this.setData({
        [e.target.dataset.itemname]:e.detail.detail.value,
      });

      console.log('e',e);
    },

  },
  lifetimes: {
    created: async function () {

    },
    attached: function () {

    },
    ready: async function () {


    },
    moved: function () {

    },
    detached: function () {

    }
  }

});